package com.example.yiqinmanagersystem.controller.api;

import com.alibaba.fastjson.JSONObject;
import com.example.yiqinmanagersystem.entity.LineTrend;
import com.example.yiqinmanagersystem.entity.NocvData;
import com.example.yiqinmanagersystem.entity.NocvGlobalData;
import com.example.yiqinmanagersystem.entity.NocvNews;
import com.example.yiqinmanagersystem.service.DataDisplayService;
import com.example.yiqinmanagersystem.service.GlobalDataDisplayService;
import com.example.yiqinmanagersystem.vo.ResponseCode;
import com.example.yiqinmanagersystem.vo.ResponseResult;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/*
 **@Author:youpeng
 **Created on 2022/9/11
 */
@RestController
@RequestMapping("/api")
public class DataDisplayController {

    @Resource
    private DataDisplayService dataDisplayService;
    @Resource
    private GlobalDataDisplayService globalDataDisplayService;
    @Resource
    private RedisTemplate redisTemplate;

    /*
    * 获取疫情地图数据
    * */
    @GetMapping("/queryMap")
    public ResponseResult queryMapData(){
        //1.redis 缓存
        if(redisTemplate!=null){
            List data = redisTemplate.opsForList().range("chinaData", 0, 33);
            //2.有缓存
            if(data!=null & data.size()>0){
//                System.out.println("map查redis");
                List<NocvData> list = JSONObject.parseArray(data.toString(), NocvData.class);
                if(list.size()!=0){
                    return new ResponseResult(ResponseCode.SUCCESS ,list.size(),list);
                }else {
                    return new ResponseResult(ResponseCode.FAILED);
                }
            }else {
//                System.out.println("map查mysql");
                //3.无缓存
                List<NocvData> list = dataDisplayService.list();
                //4.加入缓存
                redisTemplate.opsForList().leftPushAll("chinaData", list);
                redisTemplate.expire("chinaData",10, TimeUnit.SECONDS);
                if(list.size()!=0){
                    return new ResponseResult(ResponseCode.SUCCESS ,list.size(),list);
                }else {
                    return new ResponseResult(ResponseCode.FAILED);
                }
            }
        }
        //查db
        List<NocvData> list = dataDisplayService.list();
        if(list.size()!=0){
            return new ResponseResult(ResponseCode.SUCCESS ,list.size(),list);
        }else {
            return new ResponseResult(ResponseCode.FAILED);
        }
    }

    /*
    *获取疫情饼图数据
    *  */
    @GetMapping("/queryPie")
    public ResponseResult queryPieData(){
        List<NocvData> list = dataDisplayService.list();
        if(list.size()!=0){
            return new ResponseResult(ResponseCode.SUCCESS ,list.size(),list);
        }else {
            return new ResponseResult(ResponseCode.FAILED);
        }
    }

    /*
    * 获取疫情柱状图数据
    * */
    @GetMapping("/queryBar")
    public ResponseResult queryBarData(){
        //1.获取所有城市和数值数据
        List<NocvData> list = dataDisplayService.list();
        //2.所有城市数据
        ArrayList<String> cityList = new ArrayList<>();
        for (NocvData data : list) {
            cityList.add(data.getName());
        }
        //3.所有城市的数值
        ArrayList<Integer> dataList = new ArrayList<>();
        for (NocvData data : list) {
            dataList.add(data.getValue());
        }
        //4.封装map
        Map map = new HashMap<>();
        map.put("cityList",cityList);
        map.put("dataList",dataList);
        if(list.size()!=0){
            return new ResponseResult(ResponseCode.SUCCESS ,map.size(),map);
        }else {
            return new ResponseResult(ResponseCode.FAILED);
        }
    }

    /*
    * 获取疫情折线图数据
    * */
    @GetMapping("/queryLine")
    public ResponseResult queryLineData(){
        //1.近7天查询的所有数据
        List<LineTrend> list = dataDisplayService.findSevenData();
        //2.封装所有的确诊人数
        ArrayList<Integer> confirmList = new ArrayList<>();
        //3.封装所有的隔离人数
        ArrayList<Integer> isolationList = new ArrayList<>();
        //4.封装所有的治愈人数
        ArrayList<Integer> cureList = new ArrayList<>();
        //5.封装所有的死亡人数
        ArrayList<Integer> deadList = new ArrayList<>();
        //6.封装所有的疑似人数
        ArrayList<Integer> similarList = new ArrayList<>();
        for (LineTrend data : list) {
            confirmList.add(data.getConfirm());
            isolationList.add(data.getIsolation());
            cureList.add(data.getCure());
            deadList.add(data.getDead());
            similarList.add(data.getSimilar());
        }
        //7.封装到map容器中
        Map map = new HashMap<>();
        map.put("confirmList",confirmList);
        map.put("isolationList",isolationList);
        map.put("cureList",cureList);
        map.put("deadList",deadList);
        map.put("similarList",similarList);
        if(list.size()!=0){
            return new ResponseResult(ResponseCode.SUCCESS ,map.size(),map);
        }else {
            return new ResponseResult(ResponseCode.FAILED);
        }
    }

    /*
    * 获取全球疫情数据
    * */
    @GetMapping("/queryGlobalMap")
    public ResponseResult queryGlobalMapData(){
        List<NocvGlobalData> list = globalDataDisplayService.list();
        if(list.size()!=0){
            return new ResponseResult(ResponseCode.SUCCESS ,list.size(),list);
        }else {
            return new ResponseResult(ResponseCode.FAILED);
        }
    }
}
